package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.LoginInfo;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.User;
import com.itheima.pojo.UserDto;
import com.itheima.service.UserService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResult find(UserDto userDto) {
        long total = userMapper.count();
        Integer start = (userDto.getPage()-1)* userDto.getPageSize();
        List<User> list = userMapper.findBylimit(userDto, start);
        return new PageResult(total,list);
    }

    @Override
    public LoginInfo login(User user) {
        User login = userMapper.login(user);
        if(login != null){
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",login.getId());
            claims.put("username",login.getName());
            LoginInfo loginInfo = new LoginInfo(login.getId(), login.getUsername(), login.getName(), JwtUtils.createJwt( claims));
            return loginInfo;
        }
        return null;

    }
}
